﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MetroFramework.Forms;
using System.Text.RegularExpressions;
using System.Data.OleDb;
using System.Timers;
namespace ServersList1
{
    public partial class Form1 : MetroForm
    {
        Boolean download = true;
        

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
           
        }

        private void metroButton1_Click(object sender, EventArgs e)
        {

            string msg = "Servers Updated Succeffuly !";
            if (download)
            {
                gui1.Text = "Servers Download Plz Wait !";
                if (everytimeplus1())
                {
                    gui1.Text = "Data DownLoad Now";
                    MessageBox.Show("Done !");
                    listviewadd(msg);
                    MessageBox.Show("Done!");
                }
                else
                {
                    MessageBox.Show("SomeThing Wrong !");

                }





            }
           

        }
        private void dataproccess() 
        {
            //Function webpage = new Function();
            //string getpage = webpage.getserverpage("http://servers.travibot.com/?l=en&page=");
        
        
        
        
        
        }
        private Boolean everytimeplus1()
        { 
            int no1 = 33 ;
            for (int no = 1; no <= no1; no++)
            {
                Function webpage = new Function();
                string getpage = webpage.getserverpage("http://servers.travibot.com/?l=en&page=" + no );
                if (getpage != null && getpage != "")
                {


                    string pattern = "<a href=\"Http://\\w+\\d+.\\w+.\\w+/\"";
                    MatchCollection servers = Regex.Matches(getpage, pattern, RegexOptions.IgnoreCase);

                    foreach (Match serverss in servers)
                    {
                        string serverlink = serverss.Value;
                        serverlink = serverlink.Replace("<a href=\"", "");
                        serverlink = serverlink.Replace("/\"", "");
                        listView1.Items.Add(new ListViewItem(new string[] { serverlink }));
                    }
                    
                }
                
            }

            return true;
            
        }
        private void listviewadd(string msg)
        {
            // 0. DataBase Variables .
            OleDbConnection conn = new OleDbConnection();
            OleDbCommand cmd = new OleDbCommand();
            
            //1. requirements .
            conn.ConnectionString = "Provider=SQLNCLI11;Data Source=DODY\\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=allServers";
            cmd.Connection = conn;



            // 2. DbWork.
            try
            {

                conn.Open();

                foreach (ListViewItem li in listView1.Items)
                {
                    string CheckString = @"(SELECT COUNT(*) FROM ServersLists WHERE ServersUrl='"+ li.SubItems[0].Text +"')";//
                    OleDbCommand commandCheck = new OleDbCommand(CheckString, conn);
                    cmd.CommandText = "insert into ServersLists (ServersUrl)values('" + li.SubItems[0].Text + "')";
                  //  bool success = Convert.ToBoolean(cmd.ExecuteNonQuery());
                    //cmd.ExecuteNonQuery();
                    int count = (int)commandCheck.ExecuteScalar();
                    if (count>0)
                    {

                        gui1.Text = "No New Servers UnTill Now !!";


                    }
                    else 
                    {


                        cmd.ExecuteNonQuery();
                        
                    
                    
                    }
                }

                MessageBox.Show(msg);
                conn.Close();
            }
            catch (OleDbException ex)
            {

                string exx = ex.ToString();
                MessageBox.Show("Something Wrong ." + exx);


            }
        }
        
         
    }
}
